 // @ 516
// _(:з」∠)_

define([
    'Handsontable'
], function( Handsontable ) {
    'use strict';

    const Tablex = { tableArry: {} };

    // 单表
    Tablex["singleTable"] = function( param ) {
        if ( param.elmt ) {
            let elmtClass = param.elmt.id;
            if ( Tablex.tableArry[ elmtClass ] ) Tablex.tableArry[ elmtClass ].destroy();

            Tablex.tableArry[ elmtClass ] = new Handsontable( param.elmt, param.option );

            if ( param.tablePage ) Tablex.tablePage( param );
        }
    }

    // 分页
    Tablex["tablePage"] = function( param ) {
        let pageItemView = ``,
            data = param.option.data;

        for ( let x = 0; x < ( data.length / 10 ); x++ ) {
            pageItemView += `<li class="table_page_item" index="${ x }">${ x+1 }</li>`;
        }

        let pageView = `<div class="table_page">
                            <span class="table_page_prev"></span>
                            <div class="table_page_box">
                                <ul class="table_page_list"> ${ pageItemView } </ul>
                            </div>
                            <span class="table_page_next"></span>
                            <span class="table_page_totalNum">总数: ${ data.length }条</span>
                        </div>`;

        let page_ = param.elmt.parentNode.querySelector(".table_page");
        if ( page_ ) page_.remove();
        param.elmt.parentNode.insertAdjacentHTML("beforeEnd", pageView);

        let table_page = param.elmt.parentNode.querySelector(".table_page"),
            page_prev  = table_page.querySelector(".table_page_prev"),
            page_next  = table_page.querySelector(".table_page_next"),
            page_list  = table_page.querySelector(".table_page_list"),
            page_item  = table_page.querySelectorAll(".table_page_item");

        for ( let x = 0; x < page_item.length; x++ ) {
            page_item[x].addEventListener( 'click', function(e) {
                removeAllClass( "select", page_item );
                page_item[x].addClass("select");
                let index = page_item[x].getAttribute("index");
                Tablex.tableArry[ param.elmt.id ].loadData( getData( Number( index ), data ) );
            }, false);
        }
        page_item[0].click();

        page_prev.addEventListener( 'click', function(e) {
            let currentItem = page_list.querySelector(".select"),
                currentIndex = currentItem.getAttribute("index"),
                prevItem = currentItem.previousElementSibling;

            if ( prevItem ) {
                let left = page_list.offsetLeft;
                if ( Number( currentIndex ) >= 10 ) {
                    page_list.style.marginLeft = ( left + prevItem.clientWidth ) +"px";
                }
                prevItem.click();
            }
        }, false);

        page_next.addEventListener( 'click', function(e) {
            let currentItem = page_list.querySelector(".select"),
                currentIndex = currentItem.getAttribute("index"),
                nextItem = currentItem.nextElementSibling;

            if ( nextItem ) {
                let left = page_list.offsetLeft;
                if ( Number( currentIndex ) >= 9 ) {
                    page_list.style.marginLeft = ( left - nextItem.clientWidth ) +"px";
                }
                nextItem.click();
            }
        }, false);

        function getData( index, data ) {
            let currentData = [],
                current = index * 10;
                
            for ( let x = current; x < current + 10; x++ ) {
                currentData.push( data[x] );
            }

            return currentData;
        }
    }

    window.Tablex = Tablex;

    return Tablex;
});